/**
 * opportunityfollowup服务实现
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.service.impl;


import com.sxf.crm.dto.OpportunityFollowUpDTO;
import com.sxf.crm.dto.OpportunityFollowUpRequest;
import com.sxf.crm.entity.Opportunity;
import com.sxf.crm.entity.OpportunityFollowUp;
import com.sxf.crm.exception.BusinessException;
import com.sxf.crm.repository.OpportunityFollowUpRecordRepository;
import com.sxf.crm.repository.OpportunityRepository;
import com.sxf.crm.service.OpportunityFollowUpService;
import com.sxf.crm.service.UserService;

import lombok.RequiredArgsConstructor;  
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 跟进记录服务实现类
 * 
 * <p>实现商机和线索的跟进记录业务逻辑</p>
 * 
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/6/1
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OpportunityFollowUpServiceImpl implements OpportunityFollowUpService {

    private final OpportunityFollowUpRecordRepository opportunityFollowUpRecordRepository;

    private final UserService userService;

    private final OpportunityRepository opportunityRepository;

    @Override
    public List<OpportunityFollowUpDTO> listByOpportunityId(Long id) {
        List<OpportunityFollowUp> followUps = opportunityFollowUpRecordRepository.findByOpportunityIdOrderByFollowUpTimeDesc(id);
        return followUps.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    @Override
    public OpportunityFollowUpDTO addFollowUp(Long id, OpportunityFollowUpRequest request, Long currentUserId) {
        Opportunity opportunity = opportunityRepository.findById(id).orElseThrow(() -> new BusinessException("商机不存在"));

        OpportunityFollowUp followUp = new OpportunityFollowUp();
        BeanUtils.copyProperties(request, followUp);
        followUp.setOpportunityId(id);
        followUp.setCreateAt(System.currentTimeMillis());
        followUp.setCreateUserId(currentUserId);

        followUp.setUserId(request.getFollowUpUserId());

        followUp = opportunityFollowUpRecordRepository.save(followUp);

        // 更新线索的最后跟进时间和下次跟进时间
        opportunity.setLastFollowUpTime(followUp.getFollowUpTime());
        if (followUp.getNextPlanTime() != null) {
            opportunity.setNextFollowUpTime(followUp.getNextPlanTime());
        }
        opportunity.setUpdateAt(System.currentTimeMillis());
        opportunityRepository.save(opportunity);

        return convertToDTO(followUp);
    }
    
    
    private OpportunityFollowUpDTO convertToDTO(OpportunityFollowUp followUp) {
        OpportunityFollowUpDTO dto = new OpportunityFollowUpDTO();
        
        // 复制基本字段
        dto.setId(followUp.getId());
        dto.setOpportunityId(followUp.getOpportunityId());      
        dto.setContent(followUp.getContent());
        dto.setRemind(followUp.getRemind());    
        dto.setFollowUpUserId(followUp.getUserId());
        dto.setCreateUserId(followUp.getCreateUserId());
        
        // 处理枚举字段转换
        if (followUp.getMethod() != null) {
            dto.setMethod(followUp.getMethod());
        }
        
        if (followUp.getRemindType() != null) {
            dto.setRemindType(followUp.getRemindType());
        }
        
        // 处理时间字段转换 (Long -> LocalDateTime)
        if (followUp.getFollowUpTime() != null) {
            dto.setFollowUpTime(followUp.getFollowUpTime());
        }
        
        if (followUp.getNextPlanTime() != null) {
            dto.setNextPlanTime(followUp.getNextPlanTime());
        }
        
        if (followUp.getRemindTime() != null) {
            dto.setRemindTime(followUp.getRemindTime());
        }
        
        if (followUp.getCreateAt() != null) {
            dto.setCreateAt(followUp.getCreateAt());
        }
        
        if (followUp.getUserId() != null) {
            var user = userService.getUserById(followUp.getUserId());
            dto.setFollowUpUserName(user.getUsername()); 
        }

        if (followUp.getCreateUserId() != null) {
            var user = userService.getUserById(followUp.getCreateUserId());
            dto.setCreateUserName(user.getUsername()); 
        }
        
        return dto;
    }
}